package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.Util.PageResult;
import com.story.common.persistence.model.CategoryEntity;
import com.story.common.persistence.model.GgMangerEntity;
import com.story.common.persistence.model.GgMangerVo;
import com.story.core.base.controller.BaseController;
import com.story.modular.app.service.CategoryService;
import com.story.modular.app.service.GgMangerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告管理控制器
 *
 * @author fengshuonan
 * @Date 2019-12-30 18:00:35
 */
@Controller
@RequestMapping("/ggmanger")
public class GgMangerController extends BaseController {
    @Autowired
    private GgMangerService ggMangerService;
    @Autowired
    private CategoryService categoryService;

    private String PREFIX = "/app/ggmanger/";

    /**
     * 跳转到广告管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        Map<String,Object> map=new HashMap<>();
        List<CategoryEntity> list= categoryService.queryList(map);
        model.addAttribute("categorys",list);
        return PREFIX + "ggmanger.html";
    }

    /**
     * 跳转到添加广告管理
     */
    @RequestMapping("/ggmanger_add")
    public String ggmangerAdd(Model model) {
        Map<String,Object> map=new HashMap<>();
        List<CategoryEntity> list= categoryService.queryList(map);
        model.addAttribute("categorys",list);
        return PREFIX + "ggmanger_add.html";
    }

    /**
     * 跳转到修改广告管理
     */
    @RequestMapping("/ggmanger_update/{ggmangerId}")
    public String ggmangerUpdate(@PathVariable Integer ggmangerId, Model model) {
        GgMangerEntity ggMangerEntity=new GgMangerEntity();
        ggMangerEntity=ggMangerService.queryObject(ggmangerId);
        model.addAttribute("manger",ggMangerEntity);
        return PREFIX + "ggmanger_edit.html";
    }

    /**
     * 获取广告管理列表
     */
    @ApiOperation("分页广告列表 ggCategoryId：分类分类ID ，typename，广告类型 ：关键字名，searchType：查询类型，page:当前页，limit：每页记录数")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(String ggCategoryId,String typename,String stoptime,String searchType,String name,@RequestParam(required=false,defaultValue="1") int page,
                       @RequestParam(required=false,defaultValue="10") int limit) {
        Map<String,Object> map=new HashMap<String,Object>();
        String desc="";
        if ("1".equals(searchType)){
            //广告名称搜索
            //name="";
            map.put("name",name);
        }else if ("2".equals(searchType)){
            //广告名称搜索
            desc=name;
            map.put("desc",name);
        }else{
            desc=name;
        }
        if(!"true".equals(stoptime)){
            map.put("stoptime",new Date());//过期时间
            stoptime=null;
        }

        if (!"".equals(name) && name !=null){

            map.put("ggCategoryId",ggCategoryId);//广告分类
            map.put("typename",typename);//广告类型

           // map.put("searchType",searchType);
        }
        Integer cid;
        if ("".equals(ggCategoryId) || ggCategoryId ==null || "0".equals(ggCategoryId)){
            cid=null;
        }else {
            cid=Integer.parseInt(ggCategoryId);
        }

        Page<GgMangerVo> page1 = new Page<GgMangerVo>();


        page1.setCurrent(page);
        page1.setSize(limit);
        PageResult pageResult=new PageResult();
        pageResult.setData(ggMangerService.queryPageList(page1,name,typename,cid,stoptime,desc,searchType));
        pageResult.setCode("0");
        int total=page1.getTotal();
        System.out.println(total);
        pageResult.setCount(page1.getTotal());

        pageResult.setMsg("test");
        return pageResult;
    }

    /**
     * 新增广告管理
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("添加广告")
    public Object add(GgMangerEntity entity) {
        ggMangerService.save(entity);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除广告管理
     */
    @ApiOperation("删除广告详情")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam(value = "ids") String ids) {
        ggMangerService.delete(Integer.parseInt(ids));
        return SUCCESS_TIP;
    }


    /**
     * 修改广告管理
     */
    @ApiOperation("修改广告详情")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(GgMangerEntity ggMangerEntity) {
        int i=ggMangerService.update(ggMangerEntity);
        if (i>0){
            return super.SUCCESS_TIP;
        }else {
            return super.ERROR;
        }

    }

    /**
     * 广告管理详情
     */
    @ApiOperation("查看广告详情")
    @RequestMapping(value = "/detail",method = RequestMethod.POST)
    @ResponseBody
    public Object detail(Integer id) {
        return ggMangerService.queryObject(id);
    }

    /**
     * 增加点击数
     */
    @ApiOperation("添加点击数")
    @RequestMapping(value = "/addNumber",method = RequestMethod.POST)
    @ResponseBody
    public Object addNumber(Integer id) {
        GgMangerEntity ggMangerEntity=ggMangerService.queryObject(id);
        int i=ggMangerEntity.getNumber()+1;
        ggMangerEntity.setNumber(i);
        ggMangerService.update(ggMangerEntity);
        return super.SUCCESS_TIP;
    }
}
